/*
	Name: 黑红树
	Author: There,hello
	Date: 29-10-19 16:44
	Description: 
	Algorithm: 概率DP
*/
#include<bits/stdc++.h>
using namespace std;
int p,q,t,k;
int dp[2][1010101],die[2],live[2];
inline int read(){
	int x=0;
	char ch=getchar();
	while(ch<'0'||ch>'9')ch=getchar();
	while(ch>='0'&&ch<='9'){
		x=x*10+(ch^48);
		ch=getchar();
	}
	return x;
}
int gcd(int x,int y){
    int tep;
    while(y!=0){
        tep=x%y;
        x=y;
        y=tep;
    }
    return x;
}
void ready(){
     dp[0][0]=dp[1][0]=1;
     for(int i=1;i<=500000;++i){
         dp[0][i]=((long long)dp[0][i-1]*(long long)live[0])%(long long)k;
         dp[1][i]=((long long)dp[1][i-1]*(long long)live[1])%(long long)k;
     }
}
int main(){
    p=read();q=read();t=read();k=read();
    int gcd_=gcd(p,q);
    p/=gcd_;q/=gcd_;
    die[0]=2*p*p+q*q-2*p*q;
    die[1]=q*q;
    gcd_=gcd(die[0],die[1]);
    die[0]/=gcd_;die[1]/=gcd_;
    live[0]=p*(q-p)*2;
    live[1]=q*q;
    gcd_=gcd(live[0],live[1]);
    live[0]/=gcd_;live[1]/=gcd_;
    ready();
    register int x,y=0,z;
    while(t--){
        x=read();
        x-=y;
        if((x&1)||(x<=0)){
            printf("0 0\n");
            y=0;
            continue;
        }
        y=((long long)dp[0][(x>>1)-1]*die[0])%k;
        z=((long long)dp[1][(x>>1)-1]*die[1])%k;
        printf("%d %d\n",y,z);
    }
    return 0;
}